package com.ssm.comm.eureka;

import com.netflix.appinfo.MyDataCenterInstanceConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class EurekaInstanceConfig extends MyDataCenterInstanceConfig {
    private static final Logger LOG = LoggerFactory.getLogger(EurekaInstanceConfig.class);
    private String ip = null  ;
    private String id = null ;
    @Override
    public String getHostName(boolean refresh) {

        try {
            if(StringUtils.isBlank(ip)){
                ip = findFirstNonLoopbackAddress().getHostAddress() ;
            }
            return ip;
        } catch (Exception e) {
            return super.getHostName(refresh);
        }
    }

    public InetAddress findFirstNonLoopbackAddress() {
        InetAddress result = null;
        try {
            int lowest = Integer.MAX_VALUE;
            for (Enumeration<NetworkInterface> nics = NetworkInterface
                    .getNetworkInterfaces(); nics.hasMoreElements();) {
                NetworkInterface ifc = nics.nextElement();
                if (ifc.isUp()) {
                    LOG.trace("Testing interface: " + ifc.getDisplayName());
                    if (ifc.getIndex() < lowest || result == null) {
                        lowest = ifc.getIndex();
                    }
                    else if (result != null) {
                        continue;
                    }

                    // @formatter:off
                    for (Enumeration<InetAddress> addrs = ifc
                            .getInetAddresses(); addrs.hasMoreElements();) {
                        InetAddress address = addrs.nextElement();
                        if (address instanceof Inet4Address
                                && !address.isLoopbackAddress()) {
                            LOG.trace("Found non-loopback interface: "
                                    + ifc.getDisplayName());
                            result = address;
                        }
                    }
                    // @formatter:on
                }
            }
        }
        catch (IOException ex) {
            LOG.error("Cannot get first non-loopback address", ex);
        }

        if (result != null) {
            return result;
        }

        try {
            return InetAddress.getLocalHost();
        }
        catch (UnknownHostException e) {
            LOG.warn("Unable to retrieve localhost");
        }

        return null;
    }


    @Override
    public  String getInstanceId(){ // 复写他 类似于
        // eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
        if(StringUtils.isBlank(ip)){
            ip = findFirstNonLoopbackAddress().getHostAddress() ;
        }
        if(StringUtils.isBlank(id)){
            id =new StringBuilder().
                    append(ip).append(":").append(getAppname()).append(":").append(getNonSecurePort()).
                    toString().toUpperCase();
        }
        return id;
    }
}
